home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / UTIL / MEMORY / OLD / MEM208SRC / !Memphis / c / compress next >
Text File  |  1993-09-09  |  3KB  |  118 lines

  1. /*
  2.  * compress.c
  3.  * Part of the !Memphis distribution
  4.  * (c) bdb/nas, 1991-3
  5.  */
  6.  
  7. #define SQUASH
  8.  
  9. #include "kernel.h"
  10. #include "swis.h"
  11. #include "_swis.h"
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include "compress.h"
  16.  
  17. /* #define DEBUG */
  18. #ifdef DEBUG
  19. #define DEBUGF printf
  20. #else
  21. #define DEBUGF 1?(void)0:(void)printf
  22. #endif
  23.  
  24. #ifdef LZW
  25. #ifndef LZW_Compress
  26. #define LZW_Compress 0xCD000
  27. #define LZW_Decompress 0xCD001
  28. #endif
  29. void compress(char *from, int fromlen, char *to, int *tolen/*out*/)
  30. { int l;
  31.   if (_swix(LZW_Compress,_INR(0,4)|_OUT(5),3,from,to,fromlen,fromlen,&l) || l==fromlen)
  32.   { memcpy(to,from,fromlen);
  33.     l = fromlen;
  34.   }
  35.   *tolen = l;
  36. }
  37.  
  38. void uncompress(char *from, int fromlen, char *to, int tolen)
  39. { if (tolen == fromlen)
  40.   { memcpy(to,from,fromlen);
  41.     return;
  42.   }
  43.   _swix(LZW_Decompress,_INR(0,4),3,from,to,fromlen,tolen);
  44. }
  45. #endif
  46.  
  47. #ifdef SQUASH
  48. #ifndef Squash_Compress
  49. #define Squash_Compress         0x42700
  50. #define Squash_Decompress       0x42701
  51. #endif
  52.  
  53. /* Call Squash module SWI to compress data
  54.  * Assumes that tolen >= fromlen
  55.  * In the case where compressed >= exceeds uncompressed size (buffer full) then we
  56.  * simply copy the data.
  57.  * If source data < 32 bytes in length, then it is not work the overheads to compress it.
  58.  */
  59. int compress(char *from, int fromlen, char *to, int tolen)
  60. { int f,l;
  61.   char *work;
  62.  
  63.   if (fromlen >= 32)
  64.   {
  65.     _swix(Squash_Compress,_INR(0,1)|_OUT(0),8,-1,&f);
  66.     work = malloc(f);
  67.     _swix(Squash_Compress,_INR(0,5)|_OUT(0)|_OUT(5),0,work,from,fromlen,to,fromlen,&f,&l);
  68.     free(work);
  69.     if ((f == 2) || (l == 0)) /* out of output space */
  70.     {
  71.       memcpy(to,from,fromlen);
  72.       l = 0;
  73.     } 
  74.   } else
  75.   {
  76.     memcpy(to,from,fromlen);
  77.     l = 0;
  78.   }
  79.  
  80.   DEBUGF("compress: %d to %d (buf = %d)\n", fromlen, fromlen-l, tolen);
  81.   return(fromlen - l);
  82. }
  83.  
  84.  
  85. /* Decompression routine
  86.  * This has a couple of messy hacks in it. Since we don't store anywhere whether
  87.  * a page is compressed, or copied, we have to use the following rules:
  88.  *   If the 'compressed size' and the expected file size are equal, then copied
  89.  *   If 'compressed size' < 32 bytes then copied
  90.  *   If 'compressed data' starts with byte 0x1f then compressed, else copied
  91.  */
  92. void uncompress(char *from, int fromlen, char *to, int tolen)
  93. { int f,l;
  94.   char *work;
  95.   if (((tolen == fromlen) || (fromlen < 32)) && (*from != 0x1f))
  96.   {
  97.     DEBUGF("copy1: %d\n", fromlen);
  98.     memcpy(to,from,fromlen);
  99.     return;
  100.   }
  101.   if (*from == 0x1f)
  102.   {
  103.     _swix(Squash_Decompress,_INR(0,1)|_OUT(0),8,-1,&f);
  104.     work = malloc(f);
  105.     _swix(Squash_Decompress,_INR(0,5)|_OUT(0)|_OUT(5),4,work,from,fromlen,to,tolen,&f,&l);
  106.     free(work);
  107.     DEBUGF("uncompress: %d to %d\n", fromlen, tolen);
  108.   } else
  109.   {
  110.     DEBUGF("copy2: %d\n", fromlen);
  111.     memcpy(to,from,fromlen);
  112.     return;
  113.   }
  114.  
  115. }
  116.  
  117. #endif
  118.